package ltd.newbee.mall.service.impl;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.HallMapper;
import ltd.newbee.mall.dao.ScheduleMapper;
import ltd.newbee.mall.entity.Hall;
import ltd.newbee.mall.entity.Schedule;
import ltd.newbee.mall.service.HallService;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HallServicelmpl implements HallService {

    @Autowired
    HallMapper hallMapper;
    @Autowired
    ScheduleMapper scheduleMapper;

    @Override
    public PageResult selectAll(PageQueryUtil pageUtil) {
        List<Hall> halls = hallMapper.selectAll(pageUtil);
        System.out.println("halls:"+halls.get(0).getHallName());
        int total = hallMapper.getTotalSchedule();
        PageResult pageResult = new PageResult(halls, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public PageResult selectHall(PageQueryUtil pageUtil,String nowtime,String HallId) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date newtime = simpleDateFormat.parse(nowtime);
        calendar.add(Calendar.DAY_OF_MONTH, 1); //设置提前一天
        String formertime = simpleDateFormat.format(calendar.getTime());
        int hallId =Integer.parseInt(HallId);
        List<Hall> halls = scheduleMapper.selectHall(pageUtil,newtime,formertime,hallId);
        int total = hallMapper.getTotalSchedule();
        PageResult pageResult = new PageResult(halls, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveHall(Hall hall) {
        if (hallMapper.insertHall(hall) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateHall(Hall hall) {
        if(hallMapper.updateHall(hall)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public boolean deleteHall(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除分类数据
        return hallMapper.deleteHall(ids) > 0;
    }

}
